<template>
  <div class="wepage_list">
    <el-row>
      <el-col :span="8"><el-input v-model="wd" placeholder="输入标题关键字搜索" @keyup.enter.native="submitSearch"></el-input></el-col>
      <el-col :span="3" style="margin-left:10px"><el-button type="primary" @click="submitSearch">搜索</el-button></el-col>
    </el-row>
    <el-form :inline="true">
      <el-form-item label="内容性质">
        <el-select v-model="attrId" clearable>
          <el-option v-for="item,n in attrs" :value="item.id" :label="item.name" :key="item.id.toString()"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="auditStatus" clearable>
          <el-option value="0" label="待审核"></el-option>
          <el-option value="1" label="审核通过"></el-option>
          <el-option value="2" label="审核不通过"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="查看权限">
        <el-select v-model="authStatus" clearable>
          <el-option value="1" label="自己可见"></el-option>
          <el-option value="2" label="事业部"></el-option>
          <el-option value="3" label="公共"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-row>
      <el-button @click="$router.push({name:'WePage', params: {id: 'new'}})" icon="plus">新建微页面</el-button>
      <el-button @click="dialog=true" :disabled="!selection.length">移动到内容性质</el-button>
      <el-button @click="handleDelBatch" :disabled="!selection.length">批量删除</el-button>
    </el-row>
    <el-table
      v-loading="tbLoading"
      element-loading-text="拼命加载中"
      :data="pageList.list"
      stripe
      class="table"
      @select="handleSelect"
      @select-all="handleSelect">
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="二维码" class-name="is-center">
        <template slot-scope="scope">
          <el-popover
            placement="bottom"
            width="100"
            popper-class="popper_qr"
            trigger="click">
            <img class="img_qr" :src="`data:image/png;base64,${scope.row.qrCode}`" alt="">
            <i class="icon icon-erweima" slot="reference"></i>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="内容性质" prop="attrName" class-name="is-center"></el-table-column>
      <el-table-column label="查看权限/审核状态" class-name="is-center">
        <template slot-scope="scope">
          <span v-if="scope.row.commonType">公共</span>
          <span v-if="scope.row.commonType&&scope.row.groupType">,</span>
          <span v-if="scope.row.groupType">事业部</span>
          <span v-if="!scope.row.commonType&&!scope.row.groupType">自己可见</span>
          <p v-if="scope.row.auditStatus!==undefined" style="color:#999">{{scope.row.auditStatus===0?'待审核(品牌vi)':scope.row.auditStatus===1?'审核通过':'审核不通过'}}</p>
        </template>
      </el-table-column>
      <el-table-column label="发布状态" class-name="is-center">
        <template slot-scope="scope">{{scope.row.publishStatus==0?'草稿':'已发布'}}</template>
      </el-table-column>
      <el-table-column label="更新时间" class-name="is-center" width="180">
        <template slot-scope="scope">
          {{dateFormat(scope.row.updateTime)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150"  class-name="is-center">
        <template slot-scope="scope">
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-caret-bottom el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="scope.row.publishStatus" @click.native="$router.push({name: 'minPageFirst', params: {id: scope.row.id}})">报表</el-dropdown-item>
              <el-dropdown-item @click.native="$router.push({name:'WePage', params: {id: scope.row.id}})">编辑</el-dropdown-item>
              <el-dropdown-item><a :href="`${baseUrl}/content/micro/preview/${scope.row.id}`" target="_blank" style="display:block;text-decoration:none;color:#5e6b7c">预览</a></el-dropdown-item>
              <el-dropdown-item v-if="scope.row.publishStatus==0" @click.native="handlePublic(scope.row.id)">发布</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.publishStatus" @click.native="handleWxOptionClick(scope.row.id)">微信设置</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.isWxUsed" @click.native="handlePushClick(scope.row.id)">发送设置</el-dropdown-item>
              <el-dropdown-item @click.native="handleDel(scope.row.id)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        :current-page.sync="page"
        :page-size.sync="size"
        layout="sizes, total, prev, pager, next, jumper"
        :page-sizes="[10, 30, 50, 100]"
        :total="pageList.page.total"
        @current-change="submitSearch()"
        @size-change="handleSizeChange">
      </el-pagination>
    </div>
    <!-- 选择内容性质 -->
    <el-dialog title="选择内容性质" :visible.sync="dialog">
      <span>请选择内容性质：</span>
      <el-select v-model="moveToAttr">
        <el-option v-for="item,n in attrs" :value="item.id" :label="item.name" :key="item.id.toString()"></el-option>
      </el-select>
      <div slot="footer">
        <el-button type="primary" @click="handleMove" :loading="loading">确定</el-button>
        <el-button @click="dialog=false;">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 微信设置 -->
    <dialog-wx-option ref="wxOption" :id="curId" @save="submitSearch()"></dialog-wx-option>
    <!-- 发送设置 -->
    <dialog-push ref="wxPush" :id="curId"></dialog-push>
  </div>
</template>
<script>
  import {mapActions, mapState} from 'vuex'
  import config from '@/config/env'
  import dialogWxOption from '@/components/common/content/wePage/dialogWxOption'
  import dialogPush from '@/components/common/content/wePage/dialogPush'
  export default {
    components: {dialogWxOption, dialogPush},
    data () {
      return {
        wd: '',
        attrId: '',
        auditStatus: '',
        authStatus: '',
        selection: [],
        curId: null,
        page: 1,
        size: 10,
        moveToAttr: '',
        dialog: false,
        tbLoading: false,
        loading: false
      }
    },
    mounted () {
      this.initPage()
    },
    computed: {
      ...mapState({
        attrs: state => state.contentManage.contentType,
        pageList: state => state.contentManage.wePageList
      }),
      baseUrl () {
        return config.baseUrl2
      }
    },
    methods: {
      ...mapActions(['getWechatList', 'getWePageList', 'getContentTypes', 'delWePages', 'moveWepages', 'publicWepage']),
      initPage () {
        this.getContentTypes()
        this.submitSearch()
        this.getWechatList()
      },
      // 日期格式化
      dateFormat (date) {
        return this.$date.format(new Date(date), 'YYYY-MM-DD HH:mm:ss')
      },
      // 搜索
      async submitSearch () {
        this.tbLoading = true
        await this.getWePageList({
          title: this.wd,
          attrId: this.attrId,
          auditStatus: this.auditStatus,
          authStatus: this.authStatus,
          page: this.page,
          size: this.size
        })
        this.tbLoading = false
      },
      // 改变分页大小
      handleSizeChange (size) {
        this.size = size
        this.submitSearch()
      },
      // 选中行
      handleSelect (selection) {
        this.selection = selection
      },
      // 删除微页面
      async handleDel (id) {
        await this.$confirm('是否确认删除', '确认')
        const res = await this.delWePages([id])
        if (res.data.code === 1) {
          this.$message.success('删除成功')
          this.submitSearch()
        }
      },
      // 批量删除
      async handleDelBatch () {
        const selection = this.selection.map(item => item.id)
        await this.$confirm('是否确认删除', '确认')
        const res = await this.delWePages(selection)
        if (res.data.code === 1) {
          this.$message.success('删除成功')
          this.selection = []
          this.submitSearch()
        }
      },
      // 移动到内容性质
      async handleMove () {
        if (!this.moveToAttr) {
          this.$message.warning('请选择内容性质')
          return false
        }
        const selection = this.selection.map(item => item.id)
        this.loading = true
        const res = await this.moveWepages({
          chooseAttributes: this.moveToAttr,
          list: selection
        })
        this.loading = false
        if (res.data.code === 1) {
          this.$message.success('保存成功')
          this.dialog = false
          this.submitSearch()
          this.selection = []
        }
      },
      // 发布微页面
      async handlePublic (id) {
        await this.$confirm('是否确认发布', '确认')
        const res = await this.publicWepage(id)
        if (res.data.code === 1) {
          this.$message.success('发布成功')
          this.submitSearch()
        }
      },
      // 点击微信设置
      handleWxOptionClick (id) {
        this.curId = id
        this.$refs.wxOption.show = true
      },
      // 点击发送设置
      handlePushClick (id) {
        this.curId = id
        this.$refs.wxPush.show = true
      }
    }
  }
</script>
<style lang="scss">
  .wepage_list{
    .el-row{margin-bottom:15px}
    .el-dropdown-link{color:#20a0ff}
    .el-table{
      .icon{font-size:20px;cursor:pointer;}
      .el-dropdown{cursor:pointer;}
    }
  }
  .popper_qr{
    &{min-width:auto;}
    .img_qr{margin:-10px;width:120px;height:120px;}
  }
</style>
